<?php

// 图库 类

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;



class PhotosController extends Controller
{
	/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

	// 首页
	
	// 图库 列表 
	public function photoList( Request $request )
	{
		
		// 查询 图库 分类
		$category = DB::table('category as c') 
					-> where( 'c.pid','=',12 )
					// -> orderBy('c.sort','desc')
					-> get();
		$pics = array();
		foreach( $category as $key => $category_info )
		{
			// 每个分类查询 24 条
			$photos = DB::table('photos as p') 
					-> select( "p.id","p.desc",'c.name as cname','pp.pic_url','p.title')
					-> leftJoin('category as c','c.id','=','p.category_id')
					-> leftJoin('photos_pic as pp','pp.photo_id','=','p.id')
					-> where( 'p.category_id',$category_info->id )
					// 封面
					-> where( 'pp.status',1 )
					-> orderBy( 'id','desc' ) 
					-> get(24);
			$all = array();
			// 替换 图片 地址
			foreach( $photos as $ke => $info )
			{
				$photo = array();
				$photo['id']=$info->id;
				$photo['cname']=$info->cname;
				$photo['desc']=$info->desc;
				$photo['title']=$info->title;
				// 线上 图片 地址 
				$photo['pic_url'] = str_replace("/images",config('app.imgPath')."/images",$info->pic_url);
				$all[$ke]=$photo;
			}
			$all = array_chunk( $all,8 );
			$pics[$key]=$all;
		}
		
		// 三元素
		$tdk = DB::table('category as c') 
					-> where( 'c.id','=',12 )
					-> first();

        $webConfig = DB::table('webconfig')
            -> select('webName')
            -> where('id',1)
            -> first();

		return view( 'photos.list',['webConfig'=>$webConfig, 'category'=>$category,'pics'=>$pics,'tdk'=>$tdk  ] );
		
	}
	
	// 图库 分类 列表
	public function photoCategoryList( $category_id='13',Request $request )
	{
		// 默认花边
		
		// 查询每个分类 所有的相册
		$all_photos = DB::table('photos as p')
					-> select( "p.id","p.desc",'p.title','pp.pic_url')
					-> leftJoin('photos_pic as pp','pp.photo_id','=','p.id')
					-> where('p.category_id', $category_id)
					// 封面
					-> where( 'pp.status',1 )
					// 从 哪开始
					-> offset(0)
					// 只取10条
					-> limit(10)
					-> get();
		// 不在允许的范围内
		if( !in_array($category_id,array(13,14)) )
		{
			return view('errors.404');
			// abort(403);
		}
		$photos = array();
		foreach( $all_photos as $key => $photo_info )
		{
			$photo = array();
			$photo['id']=$photo_info->id;
			$photo['desc']=$photo_info->desc;
			$photo['title']=$photo_info->title;
			// 线上 图片 地址 
			$photo['pic_url'] = str_replace("/images",config('app.imgPath')."/images",$photo_info->pic_url);
			
			// 点赞 计数
			$likes = DB::table('photos')
					-> select('good')
					-> where('id','=',$photo_info->id)
					-> first();
			$photo['likes']=$likes->good;
			
			// 评论 计数
			$photo['comments_total'] = DB::table('comments')
									-> where( 'record_id','=',$photo_info->id )
									// 图库 类型
									-> where( 'type','=',3 )		
									-> count();
			
			$photos[$key]=$photo;
		}
		
		// 当前 所在 分类
		$category_info = DB::table('category')
				-> where( 'id','=',$category_id )
				-> first();
		
		// 查询 图库 分类
		$category = DB::table('category as c') 
					-> where( 'c.pid','=',12 )
					// -> orderBy('c.sort','desc')
					-> get();
		// 三元素
		$tdk = DB::table('category as c') 
					-> where( 'c.id','=',$category_id )
					-> first();

        $webConfig = DB::table('webconfig')
            -> select('webName')
            -> where('id',1)
            -> first();

		// 图库 分类 列表
		return view( 'photos.category',[ 'webConfig'=>$webConfig,'photos'=>$photos,'category_info'=>$category_info,'category'=>$category,'tdk'=>$tdk ] );
	}
	
	
	
	// 瀑布流 加载 图库
	public function pubuliu( Request $request )
	{
		// 包含 页数
		$data = $request -> except('_token');
		$page = intval( $data['page'] ) -1 ;
		$per = 8;
		$start = $per * $page;
		// 查询每个分类 瀑布流所需加载的相册
		$all_photos = DB::table('photos as p')
					-> select( "p.id","p.desc",'p.title','pp.pic_url')
					-> leftJoin('photos_pic as pp','pp.photo_id','=','p.id')
					-> where( 'p.category_id',$data['category_id'] )
					// 封面
					-> where( 'pp.status',1 )
					// 从 哪开始
					-> offset($start)
					// 只取8条
					-> limit(8)
					-> get();
		
		if( count($all_photos) < 1 )
		{
			$this -> show_msg('已全部加载');
		}
		$photos = array();
		foreach( $all_photos as $key => $photo_info )
		{
			$photo = array();
			$photo['id']=$photo_info->id;
			$photo['desc']=$photo_info->desc;
			$photo['title']=$photo_info->title;
			// 线上 图片 地址 
			$photo['pic_url'] = str_replace("/images",config('app.imgPath')."/images",$photo_info->pic_url);
			
			// 点赞 计数
			$likes = DB::table('photos')
					-> select('good')
					-> where('id','=',$photo_info->id)
					-> first();
			$photo['likes']=$likes->good;
			
			// 评论 计数
			$photo['comments_total'] = DB::table('comments')
									-> where( 'record_id','=',$photo_info->id )
									// 图库 类型
									-> where( 'type','=',3 )		
									-> count();
			
			$photos[$key]=$photo;
		}
		// 传递 数组 json 格式
		exit( json_encode($photos) );
	}
	
	
	
	
	
	
	
	
	
	
	// 图库 详情
    public function photoDetail( $photo_id='' )
    {
		// 1.相册 的主表信息
		$photo_info = DB::table('photos')
							-> where( 'id', $photo_id )
							-> first();
		
		// 相关 图集					
		$refer = $this -> getRefer( $photo_info->category_id );
		// 热门 图集
		$hots = $this -> hotPhotos();
		
		// 上一条 
		$prev_photo = DB::table('photos as p')
				-> select('p.id','p.title','pp.pic_url')
				-> leftJoin( 'photos_pic as pp','pp.photo_id','=',"p.id" )
				-> where( 'p.id','<', $photo_id )
				// 封面
				-> where( 'pp.status',1 )
				-> orderBy('p.id','desc')
				-> first();
		// 有数据
		if( !empty($prev_photo) )
		{
			$str = str_replace("/images",config('app.imgPath')."/images",$prev_photo->pic_url);
			$prev_photo -> pic_url = $str;
		}
		
		// dd($prev_photo);
		// 下一条
		$next_photo = DB::table('photos as p')
				-> select('p.id','p.title','pp.pic_url')
				-> leftJoin( 'photos_pic as pp','pp.photo_id','=',"p.id" )
				-> where( 'p.id','>', $photo_id )
				// 封面
				-> where( 'pp.status',1 )
				-> orderBy('p.id','desc')
				-> first();
		// 有数据
		if( !empty($next_photo) )
		{
			$str = str_replace("/images",config('app.imgPath')."/images",$next_photo->pic_url);
			$next_photo -> pic_url = $str;
		}
		
		// dd($next_photo);
		
		// 2.图片表信息
		$all_pics = DB::table('photos_pic as pp')
						-> where('pp.photo_id','=',$photo_id)
						-> get();
		$pics_info = array();
		foreach( $all_pics as $key => $pic_info )
		{
			$pic = array();
			// 线上
			$str = str_replace("/images",config('app.imgPath')."/images",$pic_info->pic_url);
			$pic['pic_url'] = $str;
			$pic['status'] = $pic_info->status;
			$pic['desc'] = $pic_info->desc;
			$pics_info[$key] = $pic;
		}
		
		
		// 获取 评论 
		$comments = DB::table('comments as c')
				-> select( 'u.name','c.create_at','c.content','c.id','c.user_id','c.good',DB::raw("replace(u.pic_url,'/images','".config('app.imgPath')."/images' ) as user_pic "  ) )
				-> leftJoin( 'user as u','u.id','=','c.user_id' )
				-> where( 'c.record_id','=',$photo_id )
				// 图库 类型
				-> where( 'c.type','=',3 )
				// 评论 时间
				-> orderBy('c.create_at','desc')
				-> paginate(5);
				
		// 评论 总数
		$comments_total = DB::table('comments as c')
				-> where( 'record_id','=',$photo_id )	
				// 图库 类型
				-> where( 'c.type','=',3 )					
				-> count();
		
		// 获取 评论 回复 
		$comments_reply = array();
		foreach( $comments as $key => $comment_info )
		{
			// 评论id
			$comment_id = $comment_info->id;
			$replys = DB::table('reply as r')
						-> select('r.*','u.name as username','uu.name as replyname',DB::raw("replace(uu.pic_url,'/images','".config('app.imgPath')."/images' ) as uuser_pic ") )
						// 被回复人
						-> leftJoin('user as u','u.id','=','r.user_id')
						// 回复人
						-> leftJoin('user as uu','uu.id','=','r.reply_user_id')
						-> where( 'r.comment_id','=',$comment_id )	
						// 回复 时间
						-> orderBy('r.create_at')
						-> get();
			$reply = array();
			foreach( $replys as $k => $reply_info )
			{
				
				$reply[$k]['id'] = $reply_info->id;
				$reply[$k]['comment_id'] = $reply_info->comment_id;
				$reply[$k]['good'] = $reply_info->good;
				$reply[$k]['user_id'] = $reply_info->user_id;
				$reply[$k]['content'] = $reply_info->content;
				$reply[$k]['reply_user_id'] = $reply_info->reply_user_id;
				$reply[$k]['create_at'] = $reply_info->create_at;
				$reply[$k]['username'] = $reply_info->username;
				$reply[$k]['replyname'] = $reply_info->replyname;
				$reply[$k]['uuser_pic'] = $reply_info->uuser_pic;
			}
			$comments_reply[$key] = $reply;
		}

        $webConfig = DB::table('webconfig')
            -> select('webName')
            -> where('id',1)
            -> first();

		return view( 'photos.detail',['webConfig'=>$webConfig,'photo_info'=>$photo_info,'pics_info'=>$pics_info,'comments'=>$comments,'comments_total'=>$comments_total,'comments_reply'=>$comments_reply,'refer'=>$refer,'hots'=>$hots,'prev_photo'=>$prev_photo,'next_photo'=>$next_photo ] );
		
		
    }
	
	// 相关 图集
	public function getRefer( $category_id='' )
	{
		// 1.相册 的主表信息
		$photos = DB::table('photos as p')
							-> select('p.id','p.title','pp.pic_url')
							-> leftJoin( 'photos_pic as pp','pp.photo_id','=',"p.id" )
							-> where( 'p.category_id', $category_id )
							// 封面
							-> where( 'pp.status',1 )
							-> take(6)
							-> get();
		$refer = array();
		foreach( $photos as $key => $pic_info )
		{
			$pic = array();
			// 线上
			$str = str_replace("/images",config('app.imgPath')."/images",$pic_info->pic_url);
			$pic['pic_url'] = $str;
			$pic['id'] = $pic_info->id;
			$pic['title'] = $pic_info->title;
			$refer[$key] = $pic;
		}
		
		return $refer;
	
	}
	
	
	// 热门 图集
	public function hotPhotos()
	{
		// 1.相册 的主表信息
		$photos = DB::table('photos as p')
							-> select('p.id','p.title','pp.pic_url')
							-> leftJoin( 'photos_pic as pp','pp.photo_id','=',"p.id" )
							// 封面
							-> where( 'pp.status',1 )
							// 点赞 最多
							-> orderBy( 'p.good','desc' )
							-> take(6)
							-> get();
		$hots = array();
		foreach( $photos as $key => $pic_info )
		{
			$pic = array();
			// 线上
			$str = str_replace("/images",config('app.imgPath')."/images",$pic_info->pic_url);
			$pic['pic_url'] = $str;
			$pic['id'] = $pic_info->id;
			$pic['title'] = $pic_info->title;
			$hots[$key] = $pic;
		}
		
		return $hots;
	
	}
	
	
	
	
}
